<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="$t('otherPermissionList.js')ID">
              <a-input placeholder="$t('otherPermissionList.qsr')"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="$t('otherPermissionList.zs')">
              <a-select placeholder="$t('otherPermissionList.qxz')" default-value="0">
                <a-select-option value="0">{{ $t('otherPermissionList.qb') }}</a-select-option>
                <a-select-option value="1">{{ $t('otherPermissionList.gb') }}</a-select-option>
                <a-select-option value="2">{{ $t('otherPermissionList.yxz') }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary">{{ $t('otherPermissionList.cx') }}</a-button>
              <a-button style="margin-left: 8px">{{ $t('otherPermissionList.zz') }}</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table :columns="columns" :data="loadData">

      <span slot="actions" slot-scope="text, record">
        <a-tag v-for="(action, index) in record.actionList" :key="index">{{ action.describe }}</a-tag>
      </span>

      <span slot="status" slot-scope="text">
        {{ text | statusFilter }}
      </span>

      <span slot="action" slot-scope="text, record">
        <a @click="handleEdit(record)">{{ $t('otherPermissionList.bj') }}</a>
        <a-divider type="vertical" />
        <a-dropdown>
          <a class="ant-dropdown-link">
            {{ $t('otherPermissionList.gd') }} <a-icon type="down" />
          </a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a href="javascript:;">{{ $t('otherPermissionList.xq') }}</a>
            </a-menu-item>
            <a-menu-item>
              <a href="javascript:;">{{ $t('otherPermissionList.jy') }}</a>
            </a-menu-item>
            <a-menu-item>
              <a href="javascript:;">{{ $t('otherPermissionList.sc') }}</a>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </span>
    </s-table>

    <a-modal
      title="$t('otherPermissionList.cz')"
      :width="800"
      v-model="visible"
      @ok="handleOk"
    >
      <a-form :autoFormCreate="(form)=>{this.form = form}">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="$t('otherPermissionList.wysbm')"
          hasFeedback
          validateStatus="success"
        >
          <a-input placeholder="$t('otherPermissionList.wysbm')" v-model="mdl.id" id="no" disabled="disabled" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="$t('otherPermissionList.qxmc')"
          hasFeedback
          validateStatus="success"
        >
          <a-input placeholder="$t('otherPermissionList.qygmz')" v-model="mdl.name" id="permission_name" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="$t('otherPermissionList.zs')"
          hasFeedback
          validateStatus="warning"
        >
          <a-select v-model="mdl.status">
            <a-select-option value="1">{{ $t('otherPermissionList.zc') }}</a-select-option>
            <a-select-option value="2">{{ $t('otherPermissionList.jy') }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="$t('otherPermissionList.ms')"
          hasFeedback
        >
          <a-textarea :rows="5" v-model="mdl.describe" placeholder="..." id="describe"/>
        </a-form-item>

        <a-divider />

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="$t('otherPermissionList.fyqx')"
          hasFeedback
        >
          <a-select
            style="width: 100%"
            mode="multiple"
            v-model="mdl.actions"
            :allowClear="true"
          >
            <a-select-option v-for="(action, index) in permissionList" :key="index" :value="action.value">{{ action.label }}</a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-modal>

  </a-card>
</template>

<script>
import { STable } from '@/components'

export default {
  name: 'TableList',
  components: {
    STable
  },
  data () {
    return {
      description: '列表使用场景：后台管理中的权限管理以及角色管理，可用于基于 RBAC 设计的角色权限控制，颗粒度细到每一个操作类型。',

      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: null,
      mdl: {},

      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: this.$t('otherPermissionList.wysbm'),
          dataIndex: 'id'
        },
        {
          title: this.$t('otherPermissionList.qxmc'),
          dataIndex: 'name'
        },
        {
          title: this.$t('otherPermissionList.kczqx'),
          dataIndex: 'actions',
          scopedSlots: { customRender: 'actions' }
        },
        {
          title: this.$t('otherPermissionList.zs'),
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: this.$t('otherPermissionList.cz'),
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 向后端拉取可以用的操作列表
      permissionList: null,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return this.$http.get('/permission', {
          params: Object.assign(parameter, this.queryParam)
        }).then(res => {
          const result = res.result
          result.data.map(permission => {
            permission.actionList = JSON.parse(permission.actionData)
            return permission
          })
          return result
        })
      },

      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
    statusFilter (status) {
      const statusMap = {
        1: this.$t('otherPermissionList.zc'),
        2: this.$t('otherPermissionList.jy')
      }
      return statusMap[status]
    }
  },
  created () {
    this.loadPermissionList()
  },
  methods: {
    loadPermissionList () {
      // permissionList
      new Promise(resolve => {
        const data = [
          { label: this.$t('otherPermissionList.xz'), value: 'add', defaultChecked: false },
          { label: this.$t('otherPermissionList.cx'), value: 'get', defaultChecked: false },
          { label: this.$t('otherPermissionList.xg'), value: 'update', defaultChecked: false },
          { label: this.$t('otherPermissionList.lb'), value: 'query', defaultChecked: false },
          { label: this.$t('otherPermissionList.sc'), value: 'delete', defaultChecked: false },
          { label: this.$t('otherPermissionList.dr'), value: 'import', defaultChecked: false },
          { label: this.$t('otherPermissionList.dc'), value: 'export', defaultChecked: false }
        ]
        setTimeout(resolve(data), 1500)
      }).then(res => {
        this.permissionList = res
      })
    },
    handleEdit (record) {
      this.mdl = Object.assign({}, record)
      console.log(this.mdl)
      this.visible = true
    },
    handleOk () {

    },
    onChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
